All files / src/app/api/collections/[id]/audit route.ts

100% Statements 11/11
100% Branches 4/4
100% Functions 1/1
100% Lines 10/10

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32                            10x 10x 10x 9x   9x 9x   5x 5x     5x   1x      
import { NextRequest, NextResponse } from "next/server";
import { requireAuth } from "@/lib/auth";
import { getDb } from "@/lib/db";
import { requireApiCollectionOwner } from "@/server/dal/collection-access";
import { AuditController } from "@/server/api/audit";
import { errorResponse } from "@/server/api/http-errors";
 
// Owner-only read of a collection's permanent-purge trail. 404 (absent) is
// returned BEFORE 403 (not owner) by the gate — the existence-oracle convention
// (API.md §46). The purge trail is the owner's concern; a member must not read it.
export async function GET(
  request: NextRequest,
  { params }: { params: Promise<{ id: string }> },
) {
  try {
    const { id } = await params;
    const user = await requireAuth();
    const db = getDb();
 
    const owned = await requireApiCollectionOwner(db, id, user.id);
    if (owned instanceof NextResponse) return owned; // 404 absent / 403 not owner
 
    const limitParam = request.nextUrl.searchParams.get("limit");
    const entries = await AuditController.fromDb(db).listByCollection(id, {
      limit: limitParam == null ? undefined : Number(limitParam),
    });
    return NextResponse.json(entries);
  } catch (error) {
    return errorResponse(error);
  }
}